home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / 2coldir.zip / 2COLDIR.C < prev    next >
C/C++ Source or Header  |  1988-08-10  |  6KB  |  239 lines

  1. /*
  2.  * This example illustrates the use of DosFindFirst, DosFindNext, and
  3.  * DosFindClose.  The program lists the contents of a directory.
  4.  * the parameter format is: FILELIST [-h] names...
  5.  *
  6.  * modified for 2 columns and only one switch for hidden files
  7.  * Victor Roos, TFDL/ECIT 5-8-1988
  8.  *
  9.  * Created by Microsoft Corp. 1987
  10.  * Appears also in assembler version in Iabucci's book: OS/2 prgrammers guide
  11.  */
  12.  
  13. #define INCL_ERRORS
  14. #define INCL_DOSPROCESS
  15. #define INCL_DOSFILEMGR
  16.  
  17. #include <malloc.h>
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include <os2def.h>
  21. #include <bse.h>
  22.  
  23.  
  24. /* file attribute constants */
  25.  
  26. #define    ATTR_READONLY    0x001    /* read only file */
  27. #define    ATTR_HIDDEN    0x002    /* hidden file */
  28. #define    ATTR_SYSTEM    0x004    /* system file */
  29. #define    ATTR_DIRECTORY    0x010    /* subdirectory entry */
  30. #define    ATTR_ARCHIVE    0x020    /* archive bit */
  31.  
  32. /* type of list */
  33.  
  34. #define TOTALCOLMS    2    /* number of columns */
  35.  
  36. #define RESULTBUFLEN    sizeof(FILEFINDBUF)     /* size of ResultBuf */
  37.  
  38. #define SEARCHALL    "*.*"    /* default - search for everything */
  39. #define BACKSLASH    "\\"
  40. #define    MAXPATHSIZE    128
  41. #define    FILEPATHSIZE    MAXPATHSIZE + sizeof(SEARCHALL) + 1
  42.  
  43. unsigned long        fsize;    /* global filesize variable */
  44. unsigned        nfiles;    /* number of files in directory */
  45. unsigned        ndirs;    /* number of directories in directory */
  46.  
  47. main(argc, argv)
  48.     int    argc;
  49.     NPSZ    argv[];
  50. {
  51.     HDIR        DirHandle = -1; /* use any available directory handle */
  52.     USHORT        SearchCount;    /* number of files to search for */
  53.     USHORT        Attribute = ATTR_DIRECTORY;    /* default attribute */
  54.     USHORT        rc;                /* return code */
  55.     USHORT        listType;            /* default output */
  56.     CHAR        FilePath[FILEPATHSIZE];
  57.     NPSZ        s;
  58.     PFILEFINDBUF    ResultBuf;    /* pointer to returned data */
  59.     unsigned long        tsize;    /* total K bytes in directory */
  60.  
  61.     /* parse command line for switches */
  62.  
  63.     while ((--argc > 0) && (**++argv == '-')) {
  64.         for (s = argv[0]+1; *s != '\0'; s++)
  65.  
  66.         switch(*s) {
  67.  
  68.         case 'h': Attribute |= (ATTR_HIDDEN | ATTR_SYSTEM);
  69.               break;
  70.     
  71.         default: printf("usage: ddir [ -h ] name...\n");
  72.              DosExit(EXIT_PROCESS, 0);
  73.         }
  74.     }
  75.  
  76.     /* allocate buffer for file data returned from find calls */
  77.  
  78.     if ((ResultBuf =
  79.         (PFILEFINDBUF) malloc(RESULTBUFLEN)) == NULL) {
  80.  
  81.         printf("error, not enough memory\n");
  82.         DosExit(EXIT_PROCESS, 0);
  83.     }
  84.  
  85.     do {
  86.  
  87.         if (argc > 0) {
  88.         if (strlen(*argv) > MAXPATHSIZE) {
  89.             printf("error, path too large\n");
  90.             DosExit(EXIT_PROCESS, 1);
  91.         }
  92.         else {
  93.             /* if path ends with a \, append "*.*" */
  94.  
  95.             strcpy(FilePath, *argv);
  96.             
  97.             if (FilePath[strlen(*argv) - 1] == ':')
  98.                 strcat(FilePath, BACKSLASH);
  99.             if (FilePath[strlen(*argv) - 1] == '\\')
  100.                 strcat(FilePath, SEARCHALL);
  101.         }
  102.         }
  103.         else
  104.         strcpy(FilePath, SEARCHALL);    /* search using "*.*" */
  105.  
  106.         printf("\nDirectory of %s\n\n", FilePath);
  107.         
  108.         tsize = 0;
  109.         fsize = 0;
  110.         nfiles = 0;
  111.         ndirs = 0;
  112.         SearchCount = 1;            /* search for one at a time */
  113.  
  114.         /* search for first occurance of file search pattern */
  115.  
  116.         if (rc = DosFindFirst( (PSZ) FilePath,
  117.                    &DirHandle,
  118.                    Attribute,
  119.                      ResultBuf,    /* ptr to returned data */
  120.                    RESULTBUFLEN,
  121.                    &SearchCount,
  122.                     0L )) {
  123.  
  124.         if (rc != ERROR_NO_MORE_FILES) {
  125.             printf("\nFindFirst failed, error %d\n", rc);
  126.             DosExit(EXIT_PROCESS, 1);
  127.         }
  128.  
  129.         } else {
  130.  
  131.         printDirEntry( ResultBuf );         /* print file data */
  132.         tsize = tsize + fsize;             /* count total K */
  133.  
  134.         do {
  135.  
  136.             /* search for next occurance of search pattern */
  137.  
  138.             if (rc = DosFindNext( DirHandle,
  139.                           ResultBuf,
  140.                           RESULTBUFLEN,
  141.                       &SearchCount )) {
  142.  
  143.                 if (rc != ERROR_NO_MORE_FILES)  {
  144.                 printf("DosFindNext failed, error: %d\n", rc);
  145.                 DosExit(EXIT_PROCESS, 1);
  146.                 }
  147.             }
  148.             else
  149.             /* print file data */
  150.  
  151.             printDirEntry( ResultBuf );
  152.             tsize = tsize + fsize;    /* count total K */
  153.  
  154.         
  155.             } while (SearchCount > 0);    /* when 0, no more files */
  156.         }
  157.  
  158.         DosFindClose( DirHandle );        /* free directory handle */
  159.         printf("\n\n");
  160.         printf("Total of %lu K bytes in %u files in this directory\n", tsize, nfiles);
  161.         if (ndirs > 0 )
  162.         printf("%u directories in this directory", ndirs);
  163.         printf("\n");
  164.         tsize = 0;                /* reset total Kb counter */
  165.         nfiles = 0;             /* reset file counter */
  166.         ndirs = 0;                /* reset directory counter */
  167.  
  168.         if (argc > 1)            /* if any more arguments */
  169.         argv++;             /* point to next one */
  170.  
  171.     } while (--argc > 0);            /* for all filenames */
  172. }
  173.     
  174.  
  175. /*
  176.  * This routine prints the data on the file found and returns the size of
  177.  * the file in Kbytes in global variable fsize.
  178.  */
  179.  
  180. printDirEntry(dirEntry)
  181.     PFILEFINDBUF dirEntry;
  182.  
  183. {
  184.     static int    colm = 0;
  185.     static int    nlines = 0;
  186.     char        attStr[5];
  187.     FDATE        datum;
  188.     FTIME        tijd;
  189.  
  190.        /* build attribute string */
  191.        
  192.        fsize = 0;
  193.        strcpy(attStr, "----");
  194.        if (dirEntry->attrFile * ATTR_ARCHIVE)
  195.         attStr[0] = 'a';
  196.        if (dirEntry->attrFile & ATTR_SYSTEM)
  197.         attStr[1] = 's';
  198.        if (dirEntry->attrFile & ATTR_HIDDEN)
  199.         attStr[2] = 'h';
  200.        if (dirEntry->attrFile & ATTR_READONLY)
  201.         attStr[3] = 'r';
  202.  
  203.        
  204.        printf("%-13s", dirEntry->achName);        /* print filename */
  205.        printf("%-5s", attStr);            /* print attribute */
  206.        if (dirEntry->attrFile & ATTR_DIRECTORY) {
  207.         printf("<DIR>");            /* directory */
  208.         ndirs = ndirs + 1;            /* count dirs */
  209.          }
  210.         else {
  211.         fsize = dirEntry->cbFileAlloc/1024;     /* allocated file size */
  212.         /* NOTE the use of the allocated file size to get the size in K bytes */
  213.         /* prevents the use of this program as a family API program. If this */
  214.         /* is converted with BIND to be used with MS-Dos, a 0 value for the */
  215.         /* file size is returned */
  216.            
  217.         printf("%5lu", fsize);             /* print file size */
  218.         nfiles = nfiles + 1;             /* count files */
  219.          }
  220.        datum = dirEntry->fdateLastWrite;
  221.        printf(" %2u-%2.2u-%u", datum.day, datum.month, datum.year+80);
  222.        tijd = dirEntry->ftimeLastWrite;
  223.        printf(" %2u:%2.2u", tijd.hours, tijd.minutes);
  224.   
  225.        if (++colm == TOTALCOLMS) {
  226.         printf("\n");
  227.         colm = 0;
  228.         }
  229.         else
  230.         printf(" \x0BA ");
  231.         if (++nlines == 44) {
  232.         printf("Type any character to continue");
  233.         getche();
  234.         printf("\n");
  235.         nlines = 0;
  236.         }
  237.  
  238. }
  239.